package com.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.constant.MessageConstant;
import com.pojo.Menu;
import com.result.Result;
import com.service.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/main")
public class MianController {

    @Reference
    private MenuService menuService;

    @RequestMapping("/getMenu")
    public Result getMenu() {
        try {
            // 获取登陆用户的认证信息
            User loginUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            // 登陆用户名
            String username = loginUser.getUsername();
            List<Menu> Menu = menuService.getMenuAndByUserName(username);
            // 返回给前端
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, Menu);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_MENU_FAIL);
    }


    @RequestMapping("/getUserName")
    public Result getUserName() {
        try {
            // 获取登陆用户的认证信息
            User loginUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            // 登陆用户名
            String username = loginUser.getUsername();
            // 返回给前端
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, username);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

}
